/*
 * @(#)HtmlResultTestCase.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.result;

import java.util.HashMap;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import com.opensymphony.xwork.ActionContext;

import com.nhncorp.lucy.common.config.model.ApplicationInfo;
import com.nhncorp.lucy.web.helper.ServletHelper;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo.DISPATCHER_TYPE;
import com.nhncorp.usf.core.result.direct.HtmlResult;
import junit.framework.Assert;

/**
 * {@link HtmlResult} TestCase.
 *
 * @author Web Platform Development Team
 */
public class HtmlResultTestCase {
    AbstractResult result = new HtmlResult();
    //ResultPageInfo resultPageInfo;

    @BeforeClass
    public static void initialize() throws Exception {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        ActionContext.getContext().put(ServletHelper.HTTP_REQUEST, request);
        ActionContext.getContext().put(ServletHelper.HTTP_RESPONSE, response);

        ApplicationInfo.setPageEncoding("utf-8");
    }

    @Test
    public void executeScriptResult() throws Exception {
        ResultPageInfo resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, "<script>parent.addNewStudy();</script>");
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("language", "html");

        MockHttpServletResponse response = (MockHttpServletResponse)ServletHelper.getResponse();
        try {
            result.execute(resultPageInfo, new HashMap<String, Object>());
            String contents = response.getContentAsString().trim();
            Assert.assertEquals("<script>parent.addNewStudy();</script>", contents);
        } catch(Exception except) {
            Assert.fail();
        }
    }

    @Test
    public void setHeader() throws Exception {
        ResultPageInfo resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, "<script>parent.addNewStudy();</script>");
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("language", "html");
        resultPageInfo.getProperties().setProperty("header", "HeaderName1 : HeaderValue1 \n HeaderName2 : HeaderValue2");

        MockHttpServletResponse response = (MockHttpServletResponse)ServletHelper.getResponse();
        try {
            result.setHeader(resultPageInfo);
            String header1 = (String)response.getHeader("HeaderName1");
            String header2 = (String)response.getHeader("HeaderName2");

            Assert.assertEquals("HeaderValue1", header1);
            Assert.assertEquals("HeaderValue2", header2);
        } catch(Exception except) {
            Assert.fail();
        }
    }
}
